package main

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"mogutouERP/api"
	"mogutouERP/middleware"
)

func initRouter() *gin.Engine {
	r := gin.New()
	corsConfig := cors.DefaultConfig()
	corsConfig.AllowAllOrigins = true
	corsConfig.AddAllowHeaders("Authorization")

	r.Use(cors.New(corsConfig))
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	rr := r.Group("/api/v1")
	rr.POST("/login", api.Login)
	rr.GET("/commodities", api.ListCommodities)
	rr.POST("/order/delivery", api.CreateCustormerOrderSub)

	authRouter := rr.Group("/")
	authRouter.Use(middleware.Auth)
	{
		// 用户信息
		authRouter.GET("/user", api.GetUser)
		// 登出
		authRouter.GET("/logout", api.Logout)
		// 修改密码
		authRouter.PATCH("/userPassword", api.UpdatePassword)
		// 商品查看
		authRouter.GET("/commoditiesStock", api.ListCommoditiesStock)
		authRouter.GET("/commodity/commodityType", api.ListCommodityType)
		authRouter.GET("/commodity/getLocation", api.GetLocation)
		// 配件查看
		authRouter.GET("/components", api.ListComponents)
		authRouter.GET("/componentsStock", api.ListComponentsStock)
		// 客户订单操作
		authRouter.POST("/order/custormer", api.CreateCustormerOrder)
		authRouter.GET("/orders/custormer", api.ListCustormerOrders)
		authRouter.DELETE("/order/custormer/:id", api.DeleteCustormerOrder)
		// 仓库出货
		authRouter.POST("/orderSub/custormer", api.CreateCustormerOrderSub)
		authRouter.GET("/ordersSub/custormer", api.ListCustormerOrdersSub)
		authRouter.DELETE("/orderSub/custormer/:id", api.DeleteCustormerOrderSub)
		// 采购订单操作
		authRouter.POST("/order/purchase", api.CreatePurchaseOrder)
		authRouter.GET("/orders/purchase", api.ListPurchaseOrders)
		authRouter.DELETE("/order/purchase/:id", api.DeletePurchaseOrder)
		// 配件采购操作
		authRouter.POST("/order/component", api.CreateComponentOrder)
		authRouter.GET("/orders/component", api.ListComponentOrders)
		authRouter.DELETE("/order/component/:id", api.DeleteComponentOrder)
		// 客户操作
		authRouter.GET("/customers", api.ListCustomers)
		authRouter.GET("/customer", api.GetCustomer)
		authRouter.POST("/customer", api.CreateCustomer)
		authRouter.DELETE("/customer/:id", api.DeleteCustomer)
		authRouter.PATCH("/customer/:id", api.UpdateCustomer)

		//需要admin权限的API
		adminRouter := authRouter.Group("/")
		adminRouter.Use(middleware.Admin)

		adminRouter.POST("/order/createMakeMsg", api.CreateMakeMsg)
		adminRouter.GET("/order/getMakeInfo", api.GetMakeInfo)

		adminRouter.PATCH("/updateSaleOrder/:id", api.UpdateSaleOrder)

		// 确认操作
		adminRouter.PATCH("/order/custormer/:id/confirm", api.ConfirmCustormerOrder)
		adminRouter.PATCH("/orderSub/custormer/:id/confirm", api.ConfirmCustormerOrderSub)
		adminRouter.PATCH("/order/purchase/:id/confirm", api.ConfirmPurchaseOrder)
		adminRouter.PATCH("/order/component/:id/confirm", api.ConfirmComponentOrder)

		// 用户操作
		adminRouter.GET("/users", api.ListUsers)
		adminRouter.POST("/user", api.CreateUser)
		adminRouter.DELETE("/user/:id", api.DeleteUser)
		adminRouter.PATCH("/user/:id", api.UpdateUser)
		// 商品操作
		adminRouter.POST("/commodity", api.CreateCommodity)
		adminRouter.PATCH("/commodity/:id", api.UpdateCommodity)
		adminRouter.DELETE("/commodity/:id", api.DeleteCommodity)
		adminRouter.GET("/admin/commodities", api.ListCommoditiesAsAdmin)
		// 配件操作
		adminRouter.POST("/component", api.CreateComponent)
		adminRouter.PATCH("/component/:id", api.UpdateComponent)
		adminRouter.DELETE("/component/:id", api.DeleteComponent)
		adminRouter.GET("/admin/components", api.ListComponentsAsAdmin)
		// 财务报表
		adminRouter.GET("/admin/finance/:year", api.Finance)
		// 供应商操作
		adminRouter.GET("/supplys", api.ListSupplys)
		adminRouter.GET("/supply", api.GetSupply)
		adminRouter.POST("/supply", api.CreateSupply)
		adminRouter.DELETE("/supply/:id", api.DeleteSupply)
		adminRouter.PATCH("/supply/:id", api.UpdateSupply)

	}

	return r
}
